package com.wyhello.tube4.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wyhello.tube4.dao.AppendixDAO;
import com.wyhello.tube4.pojo.Appendix;
import com.wyhello.tube4.pojo.Exchange;
import com.wyhello.tube4.pojo.Position;
import com.wyhello.tube4.pojo.Reside;
import com.wyhello.tube4.pojo.Tube;

public class AppendixDAOImpl  implements AppendixDAO{
	private SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	/**
	 * 换管记录信息的录入
	 * @param change 要录入的信息
	 * @return 返回是否录入成功
	 */
	@Override
	public boolean saveAppendix(Appendix appendix) {
		Session session = sessionFactory.getCurrentSession();
		int i = 1;
		try{
			session.save(appendix);
		}catch(Exception e){
			i=0;
		}
		if(i==1){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 对换管记录的信息进行批量删除
	 * @param ids 要删除的id
	 * @return 返回删除的条数
	 */
	@Override
	public int deleteAppendix(String ids) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "delete from exchange where id in "+ids;
		int i = session.createSQLQuery(sql).executeUpdate();
		return i;
	}
	/**
	 * 查询所有的换管记录信息
	 * @return 返回所有换管记录的信息
	 */
	@Override
	public List<Appendix> queryAppendix(int objid,String objname) {
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Appendix where objid='"+objid+"' and objname='"+objname+"'  order by uploaddate desc ";
		List<Appendix> list = session.createQuery(hql).list();
		return list;
	}
	/**
	 * 根据id查询换管记录信息
	 * @param id 要查询的id
	 * @return 为ID的换管记录信息
	 */
	@Override
	public Exchange getAppendixById(int id) {
		Session session  =  sessionFactory.getCurrentSession();
		Exchange change = (Exchange) session.get(Exchange.class, Integer.valueOf(id));
		return change;
	}
	/**
	 * 对换管记录进行修改
	 * @param change 修改后的信息
	 * @return 修改是否成功
	 */
	@Override
	public boolean updateAppendix(Exchange change) {
		Session session  =  sessionFactory.getCurrentSession();
		try{
			session.update(change);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	@Override
	public List<Reside> queryExchangeRowBySurfaceId(int surfaceid) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "SELECT * FROM reside WHERE parentId = " + surfaceid +
				" AND id IN (SELECT resideid FROM tube WHERE id IN" +
				" (SELECT tubeId FROM POSITION WHERE id IN" +
				" (SELECT positionId FROM exchange)))";
		
		List<Reside>  rows = session.createSQLQuery(sql).list();
		return rows;
	}
	
	@Override
	public List<Tube> queryExchangeTubeByRowId(int rowid) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "SELECT * FROM tube WHERE resideid = " + rowid +
				" AND id IN (SELECT tubeId FROM POSITION WHERE id IN" +
				" (SELECT positionId FROM exchange))";
		
		List<Tube>  tubes = session.createSQLQuery(sql).list();
		return tubes;
	}
	@Override
	public List<Position> queryExchangePositionByTubeId(int tubeid) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "SELECT * FROM POSITION WHERE tubeId = " + tubeid +
				" AND id IN (SELECT positionId FROM exchange)";
		
		List<Position>  positions = session.createSQLQuery(sql).list();
		return positions;
	}
	@Override
	public List<Exchange> queryExchangeByPositionId(int positionid) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "SELECT * FROM exchange WHERE positionId = " + positionid;
		
		List<Exchange>  exchanges = session.createSQLQuery(sql).list();
		return exchanges;
	}
	@Override
	public int deleteExchangeById(int id) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "delete from exchange where id = "+id;
		int i = session.createSQLQuery(sql).executeUpdate();
		return i;
	}
}
